<?php

namespace Modules\Car\Http\Controllers\Api;

use App\Helpers\Constant;
use App\Helpers\PayTrait;
use App\Helpers\ResponseData;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\DB;
use Modules\Car\Entities\CarOrder;
use Modules\Car\Entities\OrderRemark;
use Modules\Car\Http\Requests\Order\OrderRequest;

//统计完成
class PickFinishController extends Controller
{
    use ResponseData,PayTrait;
    protected function statisticsModel(){
        return CarOrder::query()
            ->where('status',Constant::ORDER_CLOSED)
            ->where('process_status','>',Constant::PROCESS_STATUS);
    }
    public function list(Request $request){
        $data = $this->statisticsModel()
            ->with('user','customer')
            ->get();
        return $this->success($data);
    }
    public function edit(OrderRequest $request){
        $data = $this->statisticsModel()
            ->with('expen')
            ->where('order_sn',$request->order_sn)
            ->first();
        return $this->success($data);
    }
    public function update(OrderRequest $request){
        $user = $request->user();
        if(!$request->filled('contents')){
            return $this->failed('contents 为必填项',201);
        }
        $order = $this->statisticsModel()
            ->where('order_sn',$request->order_sn)
            ->first();
        if($order){
            $data = [
              'uid'=>$user->id,
              'nick_name'=>$user->nick_name,
              'contents'=>$request->contents
            ];
            OrderRemark::query()->create($data);
            $order->process_status=Constant::STATISTICS_FINISHED;
            $order->pick_finish_at = now();
            $order->save();
            return $this->success('提交成功');
        }else{
            return $this->failed('订单不存在',201);
        }

    }

    /**
     * @param OrderRequest $request
     * @return mixed
     * 部分退款
     */
    public function refundOrder(OrderRequest $request){
        $user = $request->user();
        if($user->post_id!=Constant::POST_MANAGER){
            return $this->failed('权限不足，店长才能清退',201);
        }
        $order = CarOrder::query()
            //->where('status',Constant::ORDER_CLOSED)
           // ->where('process_status',Constant::STATISTICS_FINISHED)
            ->where('order_sn',$request->order_sn)
            ->first();
        if($order){
            $money = $request->money;
            info('传输的金额'.$money);
            info('订单金额'.$order->mini_pay);
            if($money>0&&$money<=$order->mini_pay){
                $result = DB::update('update car_orders set order_part_refund_total=order_part_refund_total+?,order_part_refund = ?, order_part_refund_at = ?,order_part_refund_id = ?,order_part_refund_nick_name = ?  WHERE order_part_refund = ? and order_sn = ?',
                    [$request->money,1,now(),$user->id,$user->nick_name,0,$order->order_sn]);
                if($result){
                    $total = $order->mini_pay*100;
                    $money = $money*100;
                    $this->miniRefund($order->order_sn,$order->out_trade_no,$money,$total);
                    return $this->success('订单已经退还');
                }else{
                    return $this->failed('订单部分退款错误,请检查是否已经退过',201);
                }
            }else{
                return $this->failed('输入金额有误',201);
            }
        }else{
            return $this->failed('订单不存在',201);
        }
    }
}
